# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time
from utils.operators.rich_sql_sensor import RichSqlSensor
from jms.dm.dm_outport_effect_detail_dt import jms_dm__dm_outport_effect_detail_dt

label = f"dm_outport_effect_detail_dt_{int(time())}"
timeout = timedelta(minutes=30).seconds
doris_jms_dm__dm_outport_effect_detail_dt = RichSqlSensor(
    task_id='doris_jms_dm__dm_outport_effect_detail_dt',
    pool='broker_load_pool',
    email=['suning@jtexpress.com','yl_bigdata@yl-scm.com'],
    task_concurrency=1,
    conn_id='doris',
    pre_sql=f"""
    TRUNCATE TABLE jms_dm.dm_outport_effect_detail_dt PARTITION (
        p{{{{ execution_date | cst_ds_nodash }}}}
    );
    LOAD LABEL jms_dm.{label} (
        DATA INFILE(
          "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_outport_effect_detail_dt/dt={{{{ execution_date | cst_ds }}}}/*"
        )
        INTO TABLE dm_outport_effect_detail_dt
        FORMAT AS 'PARQUET'(
             scan_date
            ,waybill_no
            ,sender_network_code
            ,sender_fran_code
            ,sender_agent_code
            ,sender_network_name
            ,sender_fran_name
            ,sender_agent_name
            ,sender_area_code
            ,sender_area_name
            ,sender_city_code
            ,sender_city_name
            ,taking_time
            ,warehouse_time
            ,first_center_code
            ,first_center_name
            ,first_center_plan_time
            ,first_center_actual_time
            ,first_center_shipment_no
            ,first_center_arrival_time
            ,first_center_send_time
            ,last_center_code
            ,last_center_name
            ,last_network_code
            ,last_network_name
            ,last_fran_code
            ,last_fran_name
            ,last_agent_code
            ,last_agent_name
            ,last_area_code
            ,last_area_name
            ,last_city_code
            ,last_city_name
            ,if_zhund
            ,if_liuc
            ,if_weid
            ,if_wand 
            ,customer_code   
            ,customer_name   
            ,ordersource_code
            ,ordersource_name
            ,taking_user_code       
            ,taking_user_name       
        )
        columns from path as (dt)
        set(
             scan_date                 =  scan_date
            ,waybill_no                =  waybill_no
            ,sender_network_code       =  sender_network_code
            ,sender_fran_code          =  sender_fran_code
            ,sender_agent_code         =  sender_agent_code
            ,sender_network_name       =  sender_network_name
            ,sender_fran_name          =  sender_fran_name
            ,sender_agent_name         =  sender_agent_name
            ,sender_area_code          =  sender_area_code
            ,sender_area_name          =  sender_area_name
            ,sender_city_code          =  sender_city_code
            ,sender_city_name          =  sender_city_name
            ,taking_time               =  taking_time
            ,warehouse_time            =  warehouse_time
            ,first_center_code         =  first_center_code
            ,first_center_name         =  first_center_name
            ,first_center_plan_time    =  first_center_plan_time
            ,first_center_actual_time  =  first_center_actual_time
            ,first_center_shipment_no  =  first_center_shipment_no
            ,first_center_arrival_time =  first_center_arrival_time
            ,first_center_send_time    =  first_center_send_time
            ,last_center_code          =  last_center_code
            ,last_center_name          =  last_center_name
            ,last_network_code         =  last_network_code
            ,last_network_name         =  last_network_name
            ,last_fran_code            =  last_fran_code
            ,last_fran_name            =  last_fran_name
            ,last_agent_code           =  last_agent_code
            ,last_agent_name           =  last_agent_name
            ,last_area_code            =  last_area_code
            ,last_area_name            =  last_area_name
            ,last_city_code            =  last_city_code
            ,last_city_name            =  last_city_name
            ,if_zhund                  =  if_zhund
            ,if_liuc                   =  if_liuc
            ,if_weid                   =  if_weid
            ,if_wand                   =  if_wand
            ,customer_code             =  customer_code
            ,customer_name             =  customer_name
            ,ordersource_code          =  ordersource_code
            ,ordersource_name          =  ordersource_name
            ,taking_user_code          =  taking_user_code
            ,taking_user_name          =  taking_user_name
            ,dt                        =  dt
        )
    )
    WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
    PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dm WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dm WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

doris_jms_dm__dm_outport_effect_detail_dt << jms_dm__dm_outport_effect_detail_dt
